﻿using System;
using System.Collections.Generic;
using System.Linq;

using FineUI;
using SysManage.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.BaseInfo;
using ZHDJ.Data.Models.Edu;

namespace SysManage.Module.BaseInfo
{
    public partial class SystemParameterList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "SystemParameterView";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        #region 数据加载
        private void LoadData()
        {
            // 权限检查
            CheckPowerWithButton("SystemParameterAdd", btnNew);


            btnNew.OnClientClick = Window1.GetShowReference("~/app/SysManage/Module/BaseInfo/SystemParameterEdit.aspx", "新增");

            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;

            BindGrid();
        }

        private void BindGrid()
        {
            IQueryable<SystemParameter> q = DB.SystemParameters;

            // 在职务名称中搜索
            string searchText = ttbSearchMessage.Text.Trim();
            if (!String.IsNullOrEmpty(searchText))
            {
                q = q.Where(t => t.ParamName.Contains(searchText) || t.ID.Contains(searchText));
            }
            // 在查询添加之后，排序和分页之前获取总记录数
            Grid1.RecordCount = q.Count();

            // 排列和分页
            q = SortAndPage<SystemParameter>(q, Grid1);

            Grid1.DataSource = q;
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("SystemParameterEdit", Grid1, "editField");
            CheckPowerWithLinkButtonField("SystemParameterDelete", Grid1, "deleteField");
        }


        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string SystemParameterId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("SystemParameterDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }
                var del = DB.SystemParameters.FirstOrDefault(t => t.ID == SystemParameterId);
                DB.SystemParameters.Remove(del);
                DB.SaveChanges();
                BindGrid();
            }
        }
        #endregion

        #region 按钮事件
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("SystemParameterDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.SystemParameters.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.SystemParameters.Remove(u));
            DB.SaveChanges();
            //DB.Users.Delete(u => ids.Contains(u.ID));


            // 重新绑定表格
            BindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void rblEnableStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }    
        #endregion

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }

    }
}
